dojo.require("dijit.form.Form");
dojo.require("dijit.form.ValidationTextBox");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.NumberTextBox");
dojo.require("dijit.form.CheckBox");
dojo.require("dijit.form.Select");
dojo.require("dijit.form.FilteringSelect");
dojo.require("dojo.data.ItemFileReadStore");


function criarAreaAtuacao(outro){

	var areas = dijit.byId("AreaAtuacao");
	if(areas){
		areas.destroy();
	}
	
	var areaSelect = dojo.doc.createElement("div");
	
	dojo.byId("areaDinamico").appendChild( areaSelect );

	if(outro){
		areas = new dijit.form.ValidationTextBox({
			id:"AreaAtuacao",
			name:"AreaAtuacao",
			required:true,
			style:{width:"200px"}
		},areaSelect);
	}
	else{
	
		areas = new dijit.form.FilteringSelect({
			store:new dojo.data.ItemFileReadStore({
					url:"${pageContext.general.APP_PATH}json/areas.json"
			}),
			query:{categoriaId:"*"},
			required:true,
			style:{width:"200px"},
			id:"AreaAtuacao",
			name:"AreaAtuacao",
			maxHeight:"200",
			onChange: function(area){				
				document.getElementById("tecn_informacao").value = dijit.byId("AreaAtuacao").item.ti[0];				
			}
		},areaSelect);
		
	}
	
	return areas;
}

dojo.addOnLoad( function(){

	categorias =new dijit.form.Select({
		onChange: function(categoriaId){
			if(categoriaId=="outra"){
				areas = criarAreaAtuacao(true);
			}
			else{
				areas = criarAreaAtuacao(false);
				
				areas.query.categoriaId = categoriaId || "*";
				areas.set("value","");
			}
		},
		style:{width:"200px"},
		required:true,
		maxHeight:"200"
	},"CategoriaSelect");

	categorias.setStore(new dojo.data.ItemFileReadStore({
			url:"${pageContext.general.APP_PATH}json/categorias.json"
	}));

});

function valida(form) {
				
	if (form.rece_informacoes.checked) {
		var email = form.Email;
		if (email.value == "") {
			alert('$pageContext.idioma["negociacao.validacao.erroCampoVazio"] ' + email.getAttribute("texto") + "!");
			return false;
		} else if ( validaEmail(email.value) != "" ) {
			var erro = "$pageContext.idioma["negociacao.validacao.erroCampo"] " + email.getAttribute("texto");
			erro += "\n$pageContext.idioma["negociacao.validacao.erro"] $pageContext.idioma['negociacao.validacao.erroEmailInvalido']";
			alert(erro);
			return false;			
		}			
	}
	if ( validaFormulario(form) ) {
		return true;
	}	
}

function asterisco_email(chk_box) {		
	if (chk_box.checked) {
		document.getElementById("email_obrigatorio").style.visibility="visible";
		dijit.byId("Email").set("required","true");
	} else {
		document.getElementById("email_obrigatorio").style.visibility="hidden";
		dijit.byId("Email").set("required","false");
	}
}

